package com.gofull.gateway.xwbank.util;

import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSON;
import com.gofull.gateway.xwbank.common.SignatureAlgorithm;
import com.gofull.gateway.xwbank.config.XwBankConstant;
import com.shove.util.UtilDate;

/* *
 *类名：XwBankSubmit
 *功能：新网托管接口请求提交类
 *详细：构造新网托管各接口表单HTML文本，获取远程HTTP数据
 *版本：2.05
 *日期：2017-05-04
 */
public class XwBankSubmit {
	private static Log log = LogFactory.getLog(XwBankSubmit.class);
	
	/**
     * 生成参数签名
     * @param sParaTemp 请求前的参数数组
     * @return 要请求的参数数组
     */
    public static Map<String, String> createPostParam(String serviceName, Map<String, String> sParaTemp) {
    	String reqData = JSON.toJSONString(sParaTemp);
    	log.info("请求参数reqData=========>:" + reqData);
    	
    	Map<String, String> result = new HashMap<String, String>();
    	try {
            String privateStr = XwBankConstant.PRIVATE_KEY;
            PrivateKey privateKey = SignatureUtils.getRsaPkcs8PrivateKey(Base64
                    .decodeBase64(privateStr));
            byte[] sign = SignatureUtils.sign(SignatureAlgorithm.SHA1WithRSA,
                    privateKey, reqData);

            //加签名
            result.put("serviceName", serviceName);
            result.put("platformNo", XwBankConstant.PLATFORM_NO);
            result.put("reqData", reqData);
            result.put("keySerial", XwBankConstant.KEY_SERIAL);
            result.put("sign", Base64.encodeBase64String(sign));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
	
	/**
     * 构造提交表单HTML数据
     * @param sParaTemp 请求参数数组
     * @param strMethod 提交方式。两个值可选：post、get
     * @param strButtonName 确认按钮显示文字
     * @param serviceName 接口名称
     * @return 提交表单HTML文本
     */
    public static String buildForm(Map<String, String> reqData, String serviceName) {
    
    	String gateway = XwBankConstant.GATEWAY_URL;
    	String strMethod = "post";
        //待请求参数数组
        Map<String, String> sPara = createPostParam(serviceName, reqData);
        List<String> keys = new ArrayList<String>(sPara.keySet());

        StringBuffer sbHtml = new StringBuffer();

        sbHtml.append("<form id=\"xwBanksubmit\" name=\"xwBanksubmit\" action=\"" + gateway
                      + "\" method=\"" + strMethod + "\">");

        for (int i = 0; i < keys.size(); i++) {
            String name = (String) keys.get(i);
            String value = (String) sPara.get(name);
            
            if(name.equals("reqData")){
            	sbHtml.append("<textarea type=\"hidden\" name=\"reqData\" rows=\"3\">" + value + "</textarea>");
            }else{
            	sbHtml.append("<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\"/>");
            }
            log.info(name+"============="+value);
        }

        //submit按钮控件请不要含有name属性
        sbHtml.append("<input type=\"submit\" value=\"ok\" style=\"display:none;\"></form>");
        sbHtml.append("<script>document.forms['xwBanksubmit'].submit();</script>");

        return sbHtml.toString();
    }
    
    /**
     * 构造请求流水号 
     * yyyyMMddHHmmss + 随机的三位数
     */
    public static String createRequestNo(){
    	return UtilDate.getOrderNum() + UtilDate.getThree();
    }

}
